import {
  ADD_GOODS_TO_CART,
  INCREMENT_CART_GOODS_COUNT,
} from './mutations_types'

export default {
  async addCart({ state, commit }, payload) {
    const index = state.cartList.findIndex(item => {
      return Object.is(item.id, payload.id)
    })

    if (index >= 0) {
      commit(INCREMENT_CART_GOODS_COUNT, index)
    } else {
      commit(ADD_GOODS_TO_CART, payload)
    }

    return '已添加至购物车'
  }
}